<template>
    <div class="city">
        <city-header></city-header>
        <city-search :cities='cities'></city-search>
        <city-list :hotCities='hotCities' :cities='cities' :letter='letter'></city-list>
        <city-alphabet :alphabetList='alphabetList' @change='handleLetterChange'></city-alphabet>
    </div>
</template>
<script>
import axios from 'axios'
import CityHeader from './components/Header'
import CitySearch from './components/Search'
import CityList from './components/List'
import CityAlphabet from './components/Alphabet'
export default {
  name: 'City',
  data () {
    return {
      hotCities: [],
      cities: {},
      alphabetList: [],
      letter: 'A'
    }
  },
  components: {
    CityHeader,
    CitySearch,
    CityList,
    CityAlphabet
  },
  watch: {
    cities () {
      const cities = this.cities
      let alphabetList = []
      for (let key in cities) {
        alphabetList.push(key)
      }
      this.alphabetList = alphabetList
    }
  },
  mounted () {
    this.getCityInfo()
  },
  methods: {
    getCityInfo () {
      axios.get('/api/city.json')
        .then(this.getCityInfoSucc)
    },
    getCityInfoSucc (res) {
      console.log(res)
      res = res.data
      if (res.ret && res.data) {
        this.hotCities = res.data.hotCities
        this.cities = res.data.cities
      }
    },
    handleLetterChange (letter) {
      this.letter = letter
    }
  }
}
</script>
<style lang="stylus" scoped>

</style>
